using System;
using System.Collections.Generic;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;
using Microsoft.Xna.Framework.Net;
using Microsoft.Xna.Framework.Storage;
using Rites.GameObjects;
using Rites.Networking;

namespace Rites
{
    public delegate void TextEventDelegate(RitesProtocol messageType, string chat);
    public delegate void TextOutputDelegate(string displayText);

    public class XNAClient : Microsoft.Xna.Framework.Game
    {
        GraphicsDeviceManager graphics;
        SpriteBatch spriteBatch;
        InputHandler inputHandler = new InputHandler();
        UIManager uiController = new UIManager();

        Rites.Game ritesGame;

        public XNAClient()
        {
            InitGraphics(1024,768);
            Content.RootDirectory = "Content";

            ritesGame = new Rites.Game();
            ritesGame.LocalPlayer = new Player();
            ritesGame.LocalPlayer.Name = Environment.MachineName.ToUpper();

            uiController.InitUI(this.graphics, ritesGame.TextEvent, ritesGame.LocalPlayer.Name);
            ritesGame.ShowText += new TextOutputDelegate(uiController.Chatbox.AddChat);
        }

        private void InitGraphics(int iWidth, int iHeight)
        {
            graphics = new GraphicsDeviceManager(this);

            foreach (DisplayMode dm in GraphicsAdapter.DefaultAdapter.SupportedDisplayModes)
            {
                // Check the width and height of each mode against the passed values
                if ((dm.Width == iWidth) && (dm.Height == iHeight))
                {
                    // The mode is supported, so set the buffer formats, apply changes and return
                    graphics.PreferredBackBufferWidth = iWidth;
                    graphics.PreferredBackBufferHeight = iHeight;
                    graphics.PreferredBackBufferFormat = SurfaceFormat.Color;
                    graphics.IsFullScreen = false;
                    graphics.ApplyChanges();
                }
            }

        }

        /// <summary>
        /// Allows the game to perform any initialization it needs to before starting to run.
        /// This is where it can query for any required services and load any non-graphic
        /// related content.  Calling base.Initialize will enumerate through any components
        /// and initialize them as well.
        /// </summary>
        protected override void Initialize()
        {
            base.Initialize();
        }

        /// <summary>
        /// LoadContent will be called once per game and is the place to load
        /// all of your content.
        /// </summary>
        protected override void LoadContent()
        {
            // Create a new SpriteBatch, which can be used to draw textures.
            spriteBatch = new SpriteBatch(GraphicsDevice);

            uiController.LoadContent(Content);
            //ritesGame.LoadContent(Content);
        }

        /// <summary>
        /// UnloadContent will be called once per game and is the place to unload
        /// all content.
        /// </summary>
        protected override void UnloadContent()
        {
            // TODO: Unload any non ContentManager content here
        }

        /// <summary>
        /// Allows the game to run logic such as updating the world,
        /// checking for collisions, gathering input, and playing audio.
        /// </summary>
        /// <param name="gameTime">Provides a snapshot of timing values.</param>
        protected override void Update(GameTime gameTime)
        {
            uiController.HandleInput(inputHandler);
            ritesGame.HandleInput(inputHandler);

            if (ritesGame.gameState == eGameState.Quitting)
                this.Exit();

            ritesGame.Update(gameTime);

            base.Update(gameTime);
        }

        /// <summary>
        /// This is called when the game should draw itself.
        /// </summary>
        /// <param name="gameTime">Provides a snapshot of timing values.</param>
        protected override void Draw(GameTime gameTime)
        {
            GraphicsDevice.Clear(Color.Black);

            spriteBatch.Begin();
            uiController.Draw(spriteBatch);
            //ritesGame.Draw(spriteBatch);
            spriteBatch.End();

            base.Draw(gameTime);
        }
    }
}
